package com.shop.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.entity.SupplierWarehouse;
import com.shop.mapper.SupplierWarehouseMapper;
import com.shop.model.PageParam;
import com.shop.model.vo.Rsp;
import com.shop.utils.Utils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * yyt(SupplierWarehouse)表服务接口
 *
 * @author makejava
 * @since 2020-11-21 10:12:58
 */
@Service
@AllArgsConstructor
public class SupplierWarehouseService {
    private final SupplierWarehouseMapper supplierWarehouseMapper;

    /**
     * @param supplierWarehouse
     * @param userId
     * @return {@link Rsp}
     * @author clh
     * @description 添加供应商
     * @date 2020/11/27 12:20
     **/
    public Rsp insertSelective(SupplierWarehouse supplierWarehouse, Integer userId,Integer supplierId) {
        supplierWarehouse.setNo(Utils.UUIDUtils());
        supplierWarehouse.setManagerId(supplierId);
        supplierWarehouse.setCreateUserId(userId);

        return supplierWarehouseMapper.insertSelective(supplierWarehouse) > 0 ? Rsp.success(null).msg("添加成功") : Rsp.fail().msg("添加失败");
    }
    /**
     * @author clh
     * @description 修改
     * @date  2020/11/27 14:23
     * @param supplierWarehouse
     * @param userId
     * @return {@link Rsp}
     **/
    public Rsp updateById(SupplierWarehouse supplierWarehouse, Integer userId) {
        supplierWarehouse.setUpdateUserId(userId);
        return supplierWarehouseMapper.updateById(supplierWarehouse) > 0 ? Rsp.success(null).msg("修改成功") : Rsp.fail().msg("修改失败");
    }
    /**
     * @author clh
     * @description 删除
     * @date  2020/11/27 14:24
     * @param supplierWarehouse
     * @return {@link Rsp}
     **/
    public Rsp deleteById(SupplierWarehouse supplierWarehouse) {

        return Rsp.success(supplierWarehouseMapper.deleteById(supplierWarehouse.getId()));
    }

    public PageInfo searchSomeByPage(PageParam<SupplierWarehouse> pageParam,Integer supplierId){
        pageParam.getModel().setManagerId(supplierId);
        //开启分页
        PageHelper.startPage(pageParam.getPageNum(),pageParam.getPageSize());

        //排序
        for (int i = 0; i <pageParam.getOrderParams().length ; i++) {
            PageHelper.orderBy(pageParam.getOrderParams()[i]);
        }
        List<SupplierWarehouse> warehouseList = supplierWarehouseMapper.searchSomeByPage(pageParam.getSuperSearchKeyWord(), pageParam.getModel());
        PageInfo<SupplierWarehouse> pageInfo = new PageInfo(warehouseList);

        return pageInfo;
    }

    public Rsp getBySupplierId(Integer id) {
        List<SupplierWarehouse> list = supplierWarehouseMapper.selectList(new QueryWrapper<SupplierWarehouse>().lambda()
                .eq(SupplierWarehouse::getManagerId, id));
        return Rsp.success(list);
    }

    public Rsp getDeliveryListByWarehouseId(Integer id) {
        return Rsp.success(supplierWarehouseMapper.getDeliveryListByWarehouseId(id));
    }

    public Rsp queryAll(){

//    public List<SupplierWarehouse> getAll(){
//
//        return supplierWarehouseMapper.getAll();
//    }
        return Rsp.success(supplierWarehouseMapper.queryAll());
    }
}
